package com.eight.uploud.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eight.common.pojo.EduTeacher;
import com.eight.common.pojo.TbCheckTeacher;
import com.eight.common.pojo.TbMember;
import com.eight.common.vo.Result;
import com.eight.uploud.mapper.CheckTeacherMapper;
import com.eight.uploud.mapper.EduTeacherMapper;
import com.eight.uploud.mapper.TbMemBerMapper;
import com.eight.uploud.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @program: qy8-education
 * @description:
 * @author: 平家祥
 * @create: 2023-06-02 17:02
 **/
@Service
public class EduTeacherServiceImpl implements EduTeacherService {

    @Autowired
    EduTeacherMapper mapper;
    @Autowired
    TbMemBerMapper tbMemBer;
    @Autowired
    CheckTeacherMapper checkTeacherMapper;
    @Override
    public Result insert(EduTeacher eduTeacher) {

        /**
         * 通过 手机号查询id
         */
        String mobile = eduTeacher.getMobile();
        QueryWrapper<TbMember> tbMemberQueryWrapper = new QueryWrapper<>();
        tbMemberQueryWrapper.eq("mobile",mobile);
        TbMember tbMember = tbMemBer.selectOne(tbMemberQueryWrapper);
        String id = tbMember.getId();
        /**
         * 判断讲师审核表中是否提交过申请
         */
        QueryWrapper<TbCheckTeacher> tbCheckTeacherQueryWrapper = new QueryWrapper<>();
        tbCheckTeacherQueryWrapper.eq("teacher_id",id);
        TbCheckTeacher tbCheckTeacher1 = checkTeacherMapper.selectOne(tbCheckTeacherQueryWrapper);

        if(tbCheckTeacher1!=null){
            return Result.success("您讲师身份已被禁用，请联系管理员重新启用");
        }else{


            /**
             * 加入审核列表
             */
            TbCheckTeacher tbCheckTeacher = new TbCheckTeacher();
            tbCheckTeacher.setTeacherId(id);
            tbCheckTeacher.setCheckStatus("2");
            tbCheckTeacher.setCheckTime(LocalDateTime.now());
            checkTeacherMapper.insert(tbCheckTeacher);

            EduTeacher eduTeacher1 = new EduTeacher();
            eduTeacher1.setId(id);
            eduTeacher1.setName(eduTeacher.getName());
            eduTeacher1.setLevel(eduTeacher.getLevel());
            eduTeacher1.setAvatar(eduTeacher.getAvatar());
            eduTeacher1.setSort(0);
            eduTeacher1.setIsDeleted(0);
            eduTeacher1.setGmtCreate(LocalDateTime.now());
            eduTeacher1.setGmtModified(LocalDateTime.now());
            eduTeacher1.setMobile(eduTeacher.getMobile());
            eduTeacher1.setEmail(eduTeacher.getEmail());
            eduTeacher1.setStatus("0");
            int insert = mapper.insert(eduTeacher1);
            return Result.success("成功");
        }

    }
}
